package com.sword.invoice.channel.lq.utils;

import com.sword.invoice.channel.lq.bo.LqBussinessHeader;
import okhttp3.*;

import javax.net.ssl.X509TrustManager;
import java.util.concurrent.TimeUnit;

/**
 * @Author zhangshiheng
 * @Date 2022-10-18 9:43
 * @Version 1.0
 **/
public class LqHttpClientHelper {

    static OkHttpClient client;

    //初始化连接池
    static {
        X509TrustManager manager = SSLSocketClientUtil.getX509TrustManager();
         client = new OkHttpClient.Builder()
                .connectTimeout(60000, TimeUnit.MILLISECONDS)
                .readTimeout(60000, TimeUnit.MILLISECONDS)
                .sslSocketFactory(SSLSocketClientUtil.getSocketFactory(manager), manager)// 忽略校验
                .hostnameVerifier(SSLSocketClientUtil.getHostnameVerifier())//忽略校验
                .connectionPool(new ConnectionPool(0, 1, TimeUnit.NANOSECONDS))
                .build();
    }


    public static String doJsonSSLPost(String url, LqBussinessHeader headerParams, String requestCipher) throws Exception
    {
        String result=null;
        // 支持https请求，绕过验证
        Response response =null;
        try {
            HttpUrl httpUrl = HttpUrl.parse(url);
            RequestBody body = RequestBody.create(MediaType.parse("text/html"), requestCipher);
            Request request = new Request.Builder()
                    .addHeader("Accept", "application/json")
                    .addHeader("Content-Type", "application/json;charset=UTF-8")
                    .addHeader("jrdwptbh", headerParams.getJrdwptbh())
                    .addHeader("sydwptbh", headerParams.getSydwptbh())
                    .addHeader("fwbm", headerParams.getFwbm())
                    .addHeader("nlbm", headerParams.getNlbm())
                    .addHeader("ylbm", headerParams.getYlbm())
                    .addHeader("sxcsbz", headerParams.getSxcsbz())

                    .url(httpUrl.newBuilder().build())
                    .post(body)
                    .build();
            response = client.newCall(request).execute();

            if (!response.isSuccessful()) {
                //状态码502、501异常
                /*if (response.code() == 500 || response.code() == 501 || response.code() == 502) {
                    throw new RetryConnectException(response.code() + "", "服务地址响应码为 " + response.code() + ": 访问服务出错，检查地址或请联系管理员");
                } else {
                    throw new EInvoiceException("服务地址响应码为 " + response.code() + ": 访问地址出错，检查地址或请联系管理员");
                }*/

            }
            result = response.body().string();
       /* } catch (EInvoiceException e){
            e.printStackTrace();
            throw new EInvoiceException(e.getMessage());
        } catch (SSLException e){
            //自身网络出口，或不在税局开通白名单，无法访问局端地址，不触发重试
            e.printStackTrace();
            throw new EInvoiceException("无法连接乐企服务地址，请联系管理员");
        }catch (RetryConnectException e) {
            //抛出正常响应报文内返回的技术异常
            e.printStackTrace();
            throw new RetryConnectException(e.getCode(),e.getMessage());
        }catch (SocketException e){
            //Connection reset
            e.printStackTrace();
//            throw new RetryConnectException(ResponseConstant.REQUEST_ERROR_101.getCode(), e.getMessage());
        }catch (SocketTimeoutException e){
            //Read timed out
            e.printStackTrace();
//            throw new RetryConnectException(ResponseConstant.REQUEST_ERROR_504.getCode(), e.getMessage());*/
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }finally {
            if(response!=null){
                response.close();
            }

        }

        return result;
    }


}
